package com.my.control;

import java.util.List;

import com.my.bean.Order;
import com.my.bean.OrderAccepted;
import com.my.bean.OrderBack;
import com.my.util.PageBean;

public class OrderAction extends BaseAction {
	private String chknumber;

	public String getChknumber() {
		return chknumber;
	}

	public void setChknumber(String chknumber) {
		this.chknumber = chknumber;
	}

	public String addOrder() throws Exception {
		String securityCode = (String) session.get("securityCode");
		if (!securityCode.equalsIgnoreCase(chknumber)) {
			request.put("message", "验证码错误！");
			return "bx";
		}
		if (order.getUser().getName().isEmpty()) {
			request.put("message", "请先输入联系人姓名！");
		} else if (order.getUser().getPhone().isEmpty()) {
			request.put("message", "手机号码不能为空！");
		} else if (order.getUser().getProvince().equals("0")) {
			request.put("message", "请选择省份！");
		} else if (order.getUser().getCity().equals("0")) {
			request.put("message", "请选择城市！");
		} else if (order.getProduct().isEmpty()) {
			request.put("message", "请输入产品型号！");
		} else if (order.getIsPeriod().isEmpty()) {
			request.put("message", "请选择是否在保修期内");
		} else {
			if (orderbo.addOrder(order)) {
				request.put("message",
						"<script laguage='JavaScript'> alert('您的报修信息已经提交，请勿重复提交!') </script>");
			} else {
				request.put("message", "提交失败！");
			}
		}
		return "bx";
	}

	public String listOrderPrepare() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<Order> list;
		list = orderbo.getAllOrderByPage(pageNo, 15);
		totalRecords = orderbo.getTotalOrderRecords().intValue();
		PageBean<Order> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("listOrderPrepare_ord");
		return "listorder";
	}

	public String searchOrder() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<Order> list;
		list = orderbo.searchOrderByPage(pageNo, 15, order);
		totalRecords = orderbo.searchTotalOrderRecords(order).intValue();
		PageBean<Order> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("searchOrder_ord");
		return "listorder";
	}

	public String orderAcceptedPrepare() throws Exception {
		order = orderbo.getOrderById(Order.class, order.getId());
		request.put("order", order);
		return "orderaccepted";
	}

	public String orderAccepted() throws Exception {
		if (orderbo.orderAccepted(orderaccepted)) {
			request.put("message",
					"<script laguage='JavaScript'> alert('受理成功!') </script>");
			return this.listOrderPrepare();
		}
		request.put("message",
				"<script laguage='JavaScript'> alert('受理失败!') </script>");
		return this.orderAcceptedPrepare();
	}

	public String listOrderPrepare2() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<Order> list;
		list = orderbo.getAllOrderByPage2(pageNo, 15);
		totalRecords = orderbo.getTotalOrderRecords2().intValue();
		PageBean<Order> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("listOrderPrepare2_ord");
		return "listorder2";
	}

	public String searchOrder2() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<Order> list;
		list = orderbo.searchOrderByPage2(pageNo, 15, order);
		totalRecords = orderbo.searchTotalOrderRecords2(order).intValue();
		PageBean<Order> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("searchOrder2_ord");
		return "listorder2";
	}

	public String searchOrderAccepted() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<OrderAccepted> list;
		list = orderbo.searchOrderByPage3(pageNo, 15, orderaccepted);
		totalRecords = orderbo.searchTotalOrderRecords3(orderaccepted)
				.intValue();
		PageBean<OrderAccepted> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("searchOrderAccepted_ord");
		return "listorder3";
	}

	public String listOrderAcceptedPrepare() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<OrderAccepted> list;
		list = orderbo.getAllOrderByPage3(pageNo, 15);
		totalRecords = orderbo.getTotalOrderRecords3().intValue();
		PageBean<OrderAccepted> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("listOrderAcceptedPrepare_ord");
		return "listorder3";
	}

	public String orderBackPrepare() throws Exception {
		orderaccepted = orderbo.getOrderAcceptedById(OrderAccepted.class,
				orderaccepted.getId());
		request.put("orderaccepted", orderaccepted);
		return "orderback";
	}

	public String orderBack() throws Exception {
		if (orderbo.addOrderBack(orderback)) {
			request.put("message",
					"<script laguage='JavaScript'> alert('反馈成功!') </script>");
			return this.listOrderAcceptedPrepare();
		}
		request.put("message",
				"<script laguage='JavaScript'> alert('反馈失败!') </script>");
		return this.orderBackPrepare();
	}

	public String listOrderBackPrepare() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<OrderBack> list;
		list = orderbo.getAllOrderByPage4(pageNo, 15);
		totalRecords = orderbo.getTotalOrderRecords4().intValue();
		PageBean<OrderBack> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("listOrderBackPrepare_ord");
		return "listorder4";
	}

	public String searchOrderBack() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<OrderBack> list;
		list = orderbo.searchOrderByPage4(pageNo, 15, orderback);
		totalRecords = orderbo.searchTotalOrderRecords4(orderback).intValue();
		PageBean<OrderBack> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("searchOrderBack_ord");
		return "listorder4";
	}
}
